package com.yx.cloud.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.yx.cloud.R;
import com.yx.cloud.bean.MusicTabBean;
import com.yx.cloud.utils.RomUtils;
import java.util.List;

public class MusicTitleTabAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    private static final String TAG = "MusicTitleTabAdapter";
    private Context context;
    private List<MusicTabBean> list;
    private MusicTitleTabAdapter.InnerItemOnclickListener listener;
    private int currentPos = 0;

    public MusicTitleTabAdapter(Context context, List<MusicTabBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.music_tab_list_item,parent,false);
        MusicTitleTabAdapter.MyViewHolder viewHolder = new MusicTitleTabAdapter.MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MusicTabBean musicTabBean = list.get(position);
        MusicTitleTabAdapter.MyViewHolder myViewHolder = (MusicTitleTabAdapter.MyViewHolder) holder;
        myViewHolder.vItemView.setOnClickListener(this);
        myViewHolder.vItemView.setTag(position);
        myViewHolder.tvMusicTitleTabName.setText(musicTabBean.getMusicTopicName());
        if(position == currentPos){
            myViewHolder.tvMusicTitleTabName.setTextColor(context.getResources().getColor(R.color.button_color2));
            myViewHolder.tvMusicTitleTabName.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            myViewHolder.vMusicTitleTabSelected.setBackgroundColor(context.getResources().getColor(R.color.button_color2));
            myViewHolder.vMusicTitleTabSelected.setVisibility(View.VISIBLE);
        }else {
            myViewHolder.tvMusicTitleTabName.setTextColor(context.getResources().getColor(R.color.textview_color1));
            myViewHolder.vMusicTitleTabSelected.setVisibility(View.INVISIBLE);
            myViewHolder.tvMusicTitleTabName.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }
        Log.d("music","position + " + position);
    }

    public void  updateData(List<MusicTabBean > list){
        this.list = list;
        notifyDataSetChanged();
    }

    public void  selectItem(int position){
        currentPos = position;
        notifyDataSetChanged();
    }

    public int getSelectedItem(){
        return currentPos;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        currentPos = (int)v.getTag();
        listener.itemClick(v,(int)v.getTag());
    }

    public interface InnerItemOnclickListener {
        void itemClick(View v,int position);
    }

    public  void setOnInnerItemOnClickListener(MusicTitleTabAdapter.InnerItemOnclickListener listener) {
        this.listener = listener;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvMusicTitleTabName;
        View vMusicTitleTabSelected;
        View vItemView;
        public MyViewHolder(View itemView) {
            super(itemView);
            vItemView = itemView;
            tvMusicTitleTabName = itemView.findViewById(R.id.tv_music_title_tab_name);
            vMusicTitleTabSelected = itemView.findViewById(R.id.v_music_title_tab_selected);
        }
    }
}
